package com.android.fragmentdemo.thread1

import org.junit.Test

class Demo1 {

    @Test
    fun testThread() {

        val counter = Counter()

        val thread0 = Thread {
            for (i in 0.until(50000)) {
                counter.increase()
            }
        }

        val thread1 = Thread {
            for (i in 0.until(50000)) {
                counter.increase()
            }
        }

        thread1.start()
        thread0.start()

        thread0.join()
        thread1.join()

        println("result:${counter.count}")
    }

    class Counter {
        var count = 0

        @Synchronized
        fun increase() {
            count++
        }

        fun increase2() {
            synchronized(Counter::class.java) {
                count++
            }
        }
    }
}